﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;

namespace WPFExcelReport
{
    public partial class TGBBS13
    {
        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
            typeof(string), typeof(TGBBS13),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGBBS13;
            if (src != null)
            {
                src.AUF = (string)e.NewValue; 
            }
        }
        #endregion
        #region ==IsProduced===========
        public static readonly DependencyProperty IsProducedProperty = DependencyProperty.Register("IsProduced",
            typeof(bool), typeof(TGBBS13),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsProducedChanged));
        public bool IsProduced
        {
            get
            {
                return (bool)GetValue(IsProducedProperty);
            }
            set
            {
                SetValue(IsProducedProperty, value);
            }
        }
        private static void OnIsProducedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGBBS13;
            if (src != null)
            {
                src.IsProduced = (bool)e.NewValue;
            }
        }
        #endregion 
        #region ==GABBS===========
        public static readonly DependencyProperty GABBSProperty = DependencyProperty.Register("GABBS",
            typeof(string), typeof(TGBBS13),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGABBSChanged));
        public string GABBS
        {
            get
            {
                return (string)GetValue(GABBSProperty);
            }
            set
            {
                SetValue(GABBSProperty, value);
            }
        }
        private static void OnGABBSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGBBS13;
            if (src != null)
            {
                src.GABBS = (string)e.NewValue;
            }
        }
        #endregion
        public TGBBS13()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGABEW();
            this.DataContext = MV;
            InitializeComponent();
            GABBS = "13";
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            Load();
        }
        string LastAUF = "";
        public void Load()
        {
            switch (app.company)
            {
                case "JLT":
                    LoadJLT();
                    break;
                case "YGR":
                    LoadYGR();
                    break;
            }
        }
        public void LoadYGR()
        {
            if (vm == null)
                return;
            if (LastAUF == AUF)
                return;
            LastAUF = AUF;
            if (AUF == null)
                return;
            IsProduced = false;
            DataRow dwData = MV.SetEnv.Tables[TB].NewRow();
            string ordertype = Helpers.Helper.Left(AUF, 1); 
            dwData["GABMANDANT"] = app.Mandant;
            dwData["GABAUFKRS"] = AUF.Substring(1, 2);
            dwData["GABAUFLFD"] = AUF.Substring(3, 6);
            dwData["GABAUFPOS"] = AUF.Substring(9, 3);
            dwData["GABBS"] = GABBS;
            DataSet SResult = ((VGABEW)MV).QueryGABEW(dwData, 4);
            MergeData(SResult);
            MessageInfo msgs = new MessageInfo();
            string msg = "";
            msgs.MSGTYPE = "GBBS13";
            msgs.Sender = this.RUNID;
            msgs.ACTION = "GBBS13";
            msgs.AUFTYP = "V";
            int idx = 0; 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                idx++;
                msgs.KRS = row["ABPAUFKRS"].ToString();
                msgs.LFD = row["ABPAUFLFD"].ToString();
                msgs.POS = row["ABPAUFPOS"].ToString();
                msgs.P1 = row["QTY"].ToString();
                msgs.P2 = Helper.Right(row["MDATE"].ToString(), 2);
                msgs.P4 = row["ABPSTKLG"].ToString();//投料比    
                switch (row["ABPPOSTYP"].ToString().Trim())
                {
                    case "FP":
                    case "MU":
                        msgs.P3 = "1-分盘";
                        break;
                    case "HX":
                        msgs.P3 = "6-回修";
                        break;
                    case "BX":
                        msgs.P3 = "4-倍捻";
                        break;
                    case "XS":
                    case "SP":
                        msgs.P3 = "3-细纱";
                        break;
                    case "HM":
                        msgs.P3 = "2-粗纱";
                        break;
                    default:
                        msgs.P3 = "6-生产";
                        break; 
                }
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", dwMaster, msg, msgs);
            } 
            if (idx > 0)
            {
                IsProduced = true;
            }
        }
        public void LoadJLT()
        { 
          
            if (AUF == null)
                return;
            if (AUF.Length < 12)
                return;
            IsProduced = false;
            DataRow dwData = MV.SetEnv.Tables[TB].NewRow();
            string ordertype = Helpers.Helper.Left(AUF, 1);

            dwData["GABMANDANT"] = app.Mandant;
            dwData["GABAUFKRS"] = AUF.Substring(1, 2);
            dwData["GABAUFLFD"] = AUF.Substring(3, 6);
            dwData["GABAUFPOS"] = AUF.Substring(9, 3);
            dwData["GABBS"] = GABBS;
            DataSet SResult = ((VGABEW)MV).QueryGABEW(dwData, 4);
            MergeData(SResult);
            MessageInfo msgs = new MessageInfo();
            string msg = "";
            msgs.MSGTYPE = "GBBS13";
            msgs.Sender = this.RUNID;
            msgs.ACTION = "GBBS13";
            msgs.AUFTYP = "V";
            int idx = 0;
            string AGANG = "";
            foreach (DataRow row in TBMain.Rows)
            {
                idx++;
                msgs.KRS = row["ABPAUFKRS"].ToString();
                msgs.LFD = row["ABPAUFLFD"].ToString();
                msgs.POS = row["ABPAUFPOS"].ToString();
                msgs.P1 = row["QTY"].ToString();
                msgs.P2 = Helper.Right(row["MDATE"].ToString(), 2);
                switch (row["ABPPOSTYP"].ToString().Trim())
                { 
                    case "MU":
                        msgs.P3 = "1-领纱";
                        break;
                    case "RS":
                        msgs.P3 = "2-染色";
                        break; 
                    default:
                        AGANG = "6-生产";
                        break;
                }
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", dwMaster, msg, msgs);
            }
           
            if (idx > 0)
            {
                IsProduced = true;
            }
        }
    }
}
